home *** CD-ROM | disk | FTP | other *** search
/ fxPAINT 1.0 / fxPAINT 1.0.iso / developers / plugins / exampleplugin / lib_source / pluginfuncs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-13  |  16.0 KB  |  469 lines

  1. /*
  2. **      $VER: PluginFuncs.c 1.00 (9.8.1999)
  3. **
  4. **      Functions for the #?.plugin
  5. **
  6. **      (C) Copyright 1999 Felix Schwarz
  7. **      All Rights Reserved.
  8. */
  9.  
  10. #define __USE_SYSBASE        // perhaps only recognized by SAS/C
  11.  
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14.  
  15. #include <proto/exec.h>
  16. #include <proto/intuition.h>
  17. #include <intuition/intuition.h>
  18. #include <libraries/gadtools.h>
  19. #include <clib/gadtools_protos.h>
  20.  
  21. #include <libraries/dos.h>
  22. #include <dos/dos.h>
  23.  
  24. #include "PluginFuncs.h"
  25. #include <fxPAINT/fxPLUGIN.h>
  26. #include <fxPAINT/fxPAINT_API.h>
  27. #include "compiler.h"
  28.  
  29. #include <fxPAINT/fxPAINT_API.c>
  30.  
  31. /************ Your structures **************/
  32. struct settings
  33. {
  34.         long xor_value;
  35. };
  36.  
  37. struct handle_struct
  38. {
  39.         struct Window *win;
  40.         struct fsbitmap *content;
  41. };
  42.  
  43. /************* INIZIALIZATION ROUTINES *****************/
  44.  
  45. /* Ok, this one is very important. It passes information about the features
  46.    of your plugin to fxPAINT and is called during fxPAINTs startup. */
  47.  
  48. struct PluginInfo * __saveds ASM FXPlug_Info(register __d1 ULONG type, register __d2 struct PluginInit *plg)
  49. {
  50.         struct PluginInfo *p_pinf=NULL;
  51.  
  52.         initfunc(plg);
  53.  
  54.         if (p_pinf=AllocVec(sizeof(struct PluginInfo), MEMF_ANY|MEMF_CLEAR))
  55.         {
  56.                 /* Version information */
  57.                 p_pinf->int_version=1;   // .. for your code
  58.                 p_pinf->int_revision=0;  // .. for your code
  59.                 p_pinf->fxp_version=1;   // .. of the used fxPAINT SDK
  60.  
  61.                 /* Various Information on the Plugin */
  62.                 p_pinf->plugin_name       ="Example Plugin";
  63.                 p_pinf->filter_name       ="XOR";
  64.                 p_pinf->saver_name        ="PPM (ExamplePlugin)";
  65.                 p_pinf->plugin_description="Example Plugin: XOR, loader, saver, launchable";
  66.                 p_pinf->author_name       ="Felix Schwarz";
  67.                 p_pinf->author_company    ="Innovative";
  68.                 p_pinf->author_copyright  ="©1999 by Felix Schwarz/Innovative";
  69.                 p_pinf->logo_file         ="Plugins/Logo/examplePLUG";
  70.                 p_pinf->help_file         =NULL; /* No help file, otherwhise specify
  71.                                                     something like "PROGDIR:Plugins/Help/example/example.guide"
  72.                                                  */
  73.  
  74.                 /* Detailed information on the kind of plugin */
  75.                 /* Note: You can combine several types or take only one :) */
  76.                 p_pinf->plugin_kind  = PLG_FILTER|PLG_LOADER|PLG_SAVER|PLG_HAVEPORTS|PLG_LAUNCHABLE;
  77.                 //p_pinf->plugin_flags = PLG_FLG_LAUNCHMEATSTART; /* Start the FXPlug_Launch function right
  78.                 //                                                   after fxPAINT has fully started up ! */
  79.  
  80.                 if (type==0)
  81.                 {
  82.                         /* This call is unique */
  83.                         p_pinf->plugin_space = AllocVec(sizeof(struct handle_struct),MEMF_ANY|MEMF_CLEAR); /* Additional needed space ..
  84.                                                                                                               is freed in FXPlug_Bye and
  85.                                                                                                               can e.g. contain a structure,
  86.                                                                                                               that contains pointers to windows,
  87.                                                                                                               ports, etc. .. */
  88.                 }
  89.                 else
  90.                 {
  91.                         /* This call isn`t unique! DON`T ALLOCATE plugin_space!*/
  92.  
  93.                 }
  94.         }
  95.  
  96.         return(p_pinf);
  97. }
  98.  
  99. /* This clears the memory, that was used to pass the info. */
  100.  
  101. void __saveds ASM FXPlug_FreeInfo(register __d1 APTR mem, register __d2 struct PluginInit *plg)
  102. {
  103.         initfunc(plg);
  104.  
  105.         if (mem)
  106.         {
  107.                 FreeVec(mem);
  108.         }
  109. }
  110.  
  111. /* This function is the last one called, before fxPAINT quits */
  112.  
  113. void __saveds ASM FXPlug_Bye(register __d1 struct PluginBye *plb)
  114. {
  115.         struct handle_struct *hs;
  116.         initfunc(plb->pli);
  117.  
  118.         if (plb->allocatedmem)
  119.         {
  120.                 /* The adress, that was p_pinf->plugin_space once .. if you have
  121.                    any OS-ressources`s adresses in here, like that of windows, ports,
  122.                    etc., free them now and then free the memory. */
  123.  
  124.                 hs=plb->allocatedmem;
  125.  
  126.                 if (hs->win)
  127.                 {
  128.                         remport_sigmask(hs->win->UserPort->mp_SigBit, plb->pli->pluginid);
  129.                         CloseWindow(hs->win);
  130.                         hs->win=NULL;
  131.                 }
  132.  
  133.                 if (hs->content)
  134.                 {
  135.                         fs_freebitmap(hs->content);
  136.                         hs->content=NULL;
  137.                 }
  138.  
  139.                 FreeVec(plb->allocatedmem);
  140.         }
  141. }
  142.  
  143. /************************** EFFECTS *********************/
  144.  
  145. /* Initialize the plugin for the effects */
  146.  
  147. APTR __saveds ASM FXPlug_InitPlugin(register __d1 struct PluginInit *plg)
  148. {
  149.         /* Return address of space allocated for saving
  150.            infos of the Plugin to */
  151.  
  152.         initfunc(plg);
  153.  
  154.         return (CstAllocVec(sizeof(struct settings),MEMF_ANY|MEMF_CLEAR));
  155. }
  156.  
  157. /* .. and free any memory, that was allocated for the effect. Very useful,
  158.    if you are using structures to store pointers in it :) */
  159.  
  160. void __saveds ASM FXPlug_ClosePlugin(register __d1 APTR mem, register __d2 struct PluginInit *plg)
  161. {
  162.         /* Free all space allocated with FXPlug_InitPlug */
  163.         initfunc(plg);
  164.         if (mem)
  165.         {
  166.                 CstFreeVec(mem);
  167.         }
  168. }
  169.  
  170. /* Filter
  171.    If the Plugin is a filter, you`ll need to fill this section with
  172.    your own routines */
  173.  
  174. void __saveds ASM FXPlug_Filter(register __d1 struct PluginFilter *plf)
  175. {
  176.         APTR  myplugdata;
  177.         UBYTE *p_t, *p_f;
  178.         long max, i;
  179.  
  180.         struct fsbitmap *fsb;
  181.         struct settings *st=plf->PluginData;
  182.  
  183.         initfunc(plf->pli);
  184.  
  185.         myplugdata =plf->PluginData;
  186.         fsb        =plf->fsb;
  187.  
  188.         max=fsb->width*fsb->height*3;
  189.  
  190.         p_t = p_f = (UBYTE *) fsb->location;
  191.  
  192.         /* Of course you could replace this with a more efficient
  193.            routine, but for showing on how to do filters, this code
  194.            should be understandable :) */
  195.  
  196.         for (i=0;i<max;i++)
  197.         {
  198.                 *p_t++ = *p_f++ ^ st->xor_value;
  199.         }
  200. }
  201.  
  202. /* .. and here is the GUI-Stuff for our XOR-effect .. */
  203.  
  204. #define GAD_XOR_VAL 1L
  205. #define GAD_XOR_PER 2L
  206.  
  207. void __saveds ASM FXPlug_OpenFilterGUI (register __d1 struct PluginGUI *pog)
  208. {
  209.         struct Gadget *gads;
  210.         long win      = pog->yourwin;
  211.         long gui_ghei = pog->gui_ghei;
  212.         long gui_gsep = pog->gui_gsep;
  213.         struct settings *st=pog->PluginData;
  214.         FXGUI *fx_gui;
  215.  
  216.         initfunc(pog->pli);
  217.         fx_gui=pog->fxp_gui;
  218.  
  219.         gads=gui_crflexgad(&fx_gui->glist, win, GAD_DISTX, GAD_DISTY+5, 10, 200,
  220.                            GAD_XOR_VAL, GUI_SLIDER       ,      0,    0,  255,st->xor_value, 0, "XOR value" ,FLEX_NEXT,
  221.                            0L         , GUI_VERTSPACE    ,      0,    4,    4,            4, 0, ""          ,FLEX_NEXT,
  222.                            GAD_XOR_PER, GUI_ONEBUTTONBAR ,      0,    1,  100,            0,40, "Perform"   ,FLEX_LAST);
  223.  
  224.         if (fx_gui->win=gui_openfix("XOR",STD_SIZESLIDE_IDCMP,win,0,0,0,0))
  225.         {
  226.                 GT_RefreshWindow(fx_gui->win, NULL);
  227.                 gui_drboxsel    (win, GAD_DISTX, GAD_DISTY,
  228.                                       fx_gui->min_iw-4,
  229.                                       fx_gui->min_ih-3-2-((gui_ghei+gui_gsep)*1));
  230.         }
  231. }
  232.  
  233. void __saveds ASM FXPlug_HandleFilterGUI (register __d1 struct PluginGUI *pog)
  234. {
  235.         struct IntuiMessage *imsg;
  236.         ULONG imsgClass;
  237.         UWORD imsgCode;
  238.         BOOL realtime = pog->realtime;
  239.         struct settings *st=pog->PluginData;
  240.         FXGUI *fx_gui;
  241.  
  242.         initfunc(pog->pli);
  243.         fx_gui=pog->fxp_gui;
  244.  
  245.         imsg=pog->imsg;
  246.  
  247.         imsgClass = imsg->Class;
  248.         imsgCode = imsg->Code;
  249.         GT_ReplyIMsg(imsg);
  250.  
  251.         switch (imsgClass)
  252.         {
  253.                 case IDCMP_IDCMPUPDATE:
  254.                 case IDCMP_GADGETDOWN:
  255.                 case IDCMP_GADGETUP:
  256.                         switch(GADGETID(imsg))
  257.                         {
  258.                                 case GAD_XOR_VAL:
  259.                                         st->xor_value = gui_get_slider(get_myfxwinid(), GAD_XOR_VAL);
  260.                                         if (!realtime) {break;}
  261.  
  262.                                 case GAD_XOR_PER:
  263.                                         fx_recalc();
  264.                                 break;
  265.                         }
  266.                 break;
  267.  
  268.                 case IDCMP_CLOSEWINDOW:
  269.                         gui_closenfree(get_myfxwinid());
  270.                 break;
  271.         }
  272. }
  273.  
  274. void __saveds ASM FXPlug_CloseFilterGUI (register __d1 struct PluginGUI *pog)
  275. {
  276.         /* Optional by now .. not used! */
  277. }
  278.  
  279. /********* I/O Stuff - additional loaders and savers as Plugin ************/
  280. /**** Loader ****/
  281.  
  282. /* This routine is for checking, whether a file can be read by our plugin. */
  283. struct PluginImageInfo * __saveds ASM FXPlug_Identify_Image(register __d1 struct PluginIdent *pid)
  284. {
  285.         struct PluginImageInfo *pii=NULL;
  286.  
  287.         initfunc(pid->pli);
  288.  
  289.         /* Search "Example" within the first ten characters */
  290.         if (fx_find_str_in_buf(pid->header_data, 10, "Example"))
  291.         {
  292.                 /* Found ! */
  293.                 if (pii=CstAllocVec(sizeof(struct PluginImageInfo), MEMF_ANY|MEMF_CLEAR))
  294.                 {
  295.                         pii->identified=TRUE;
  296.                         pii->size_identified=FALSE;
  297.                 }
  298.         }
  299.  
  300.         /* You can also simply return NULL, if you couldn`t identify the pic! */
  301.         return(pii);
  302. }
  303.  
  304. /* This routine finally loads an image identified with FXPlug_Identify_Image */
  305. struct fsbitmap * __saveds ASM FXPlug_Load_Image(register __d1 struct PluginLoadImage *pli)
  306. {
  307.         struct fsbitmap *fsb=NULL;
  308.         UBYTE *p_t;
  309.         long i, max;
  310.  
  311.         initfunc(pli->pli);
  312.  
  313.         /* Allocate fsbitmap with 100 * 100 pixels ! The
  314.            returned fsbitmap HAS to be allocated this way!! */
  315.         if (fsb=fs_allocbitmap(256,256,3))
  316.         {
  317.                 p_t = (UBYTE *) fsb->location;
  318.  
  319.                 max=256*256;
  320.  
  321.                 for (i=0;i<max;i++)
  322.                 {
  323.                         *p_t++ = ((i*255)/max);       /* R */
  324.                         *p_t++ = 128;                 /* G */
  325.                         *p_t++ = (255-((i*255)/max)); /* B */
  326.                 }
  327.         }
  328.  
  329.         return(fsb);
  330. }
  331.  
  332. /**** Image Saver ****/
  333. /* This one is really easy! Save an image in the format your plugin may or may not support. */
  334. BOOL __saveds ASM FXPlug_Save_Image(register __d1 struct PluginSaveImage *psi)
  335. {
  336.         char *file_name=psi->file_name;
  337.         struct fsbitmap *fsb=psi->fsb;
  338.         BPTR p_out;
  339.  
  340.         initfunc(psi->pli);
  341.  
  342.         if (p_out=Open(file_name, MODE_READWRITE))
  343.         {
  344.                 FPrintf(p_out,"P6\n%ld %ld\n# Written by example.plugin\n255\n",
  345.                         fsb->width,
  346.                         fsb->height);
  347.                 FWrite(p_out, fsb->location, fsb->width * fsb->height * fsb->type, 1);
  348.                 Close(p_out);
  349.                 return(TRUE);
  350.         }
  351.  
  352.         return(FALSE);
  353. }
  354.  
  355. /**** Custom GUI-routine for handling events, if you added message-port ****
  356.  **** to the internal fxPAINT-routines!                                 ****/
  357. void __saveds ASM FXPlug_Handle_Ports(register __d1 struct PluginHandlePorts *php)
  358. {
  359.         struct IntuiMessage *imsg;
  360.         ULONG imsgClass;
  361.         UWORD imsgCode;
  362.         struct handle_struct *hs;
  363.  
  364.         initfunc(php->pli);
  365.         if (hs=php->pli->plugin_space)
  366.         {
  367.                 if (hs->win)
  368.                 {
  369.                         while (hs->win!=NULL ?
  370.                                              (imsg = GT_GetIMsg(hs->win->UserPort))
  371.                                              : FALSE)
  372.                         {
  373.                                 imsgClass = imsg->Class;
  374.                                 imsgCode = imsg->Code;
  375.                                 GT_ReplyIMsg(imsg);
  376.  
  377.                                 switch (imsgClass)
  378.                                 {
  379.                                         case IDCMP_CLOSEWINDOW:
  380.                                                 remport_sigmask(hs->win->UserPort->mp_SigBit,php->pli->pluginid);
  381.                                                 CloseWindow(hs->win);
  382.                                                 hs->win=NULL;
  383.                                         break;
  384.  
  385.                                         case IDCMP_NEWSIZE:
  386.                                                 fxp_drawscaledfsb(hs->win, get_gui_screen(), hs->content, 0, 0, hs->win->GZZWidth, hs->win->GZZHeight);
  387.                                         break;
  388.                                 }
  389.                         }
  390.                 }
  391.         }
  392. }
  393.  
  394. /**** Launchable Plugins - e.g. for launching the Plugin`s GUI ****/
  395. BOOL __saveds ASM FXPlug_Launch(register __d1 struct PluginLaunch *pll)
  396. {
  397.         struct handle_struct *hs;
  398.  
  399.         initfunc(pll->pli);
  400.         if (hs=pll->pli->plugin_space)
  401.         {
  402.                 if (!hs->content)
  403.                 {
  404.                         hs->content=fxp_loadpic("GUIpics/About.gfx");
  405.                 }
  406.  
  407.                 if ((!hs->win) && (hs->content))
  408.                 {
  409.                         hs->win = OpenWindowTags(
  410.                                   NULL,
  411.                                   WA_InnerWidth, hs->content->width,
  412.                                   WA_InnerHeight, hs->content->height,
  413.                                   WA_Top, 10,
  414.                                   WA_Left,10,
  415.                                   WA_MaxWidth,  ~0,
  416.                                   WA_MaxHeight, ~0,
  417.                                   WA_Activate, TRUE,
  418.                                   WA_DragBar, TRUE,
  419.                                   WA_DepthGadget, TRUE,
  420.                                   WA_CloseGadget, TRUE,
  421.                                   WA_SizeGadget, TRUE,
  422.                                   WA_SmartRefresh, TRUE,
  423.                                   WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE,
  424.                                   WA_Title, "example.plugin · Only fxPAINT makes it possible!",
  425.                                   WA_GimmeZeroZero, TRUE,
  426.                                   WA_CustomScreen, get_gui_screen(),
  427.                                   WA_ScreenTitle, "example.plugin-window :)",
  428.                                   TAG_DONE, 0);
  429.  
  430.                         if (hs->win)
  431.                         {
  432.                                 addport_sigmask(hs->win->UserPort->mp_SigBit, pll->pli->pluginid);
  433.                                 fxp_drawfsb(hs->win, get_gui_screen(), hs->content, 0, 0);
  434.                                 return(TRUE);
  435.                         }
  436.                 }
  437.         }
  438.         return(FALSE);
  439. }
  440.  
  441. /**** Display Driver - sounds important and in fact, it is :)
  442.       If you want to write own, fast(er) replacement routines for
  443.       fxPAINT screen drawing routines or want to add some new stuff,
  444.       this is your chance! If you should encounter problems such as
  445.       fxPAINT crashing when it uses your routines, try the SAS/C-compiler-
  446.       flags structureequivalents and nostackcheck ..
  447.       If fsb->type==3 -> RGB-truecolor-data
  448.       If fsb->type==1 -> Grayscale-data
  449. *****/
  450.  
  451. BOOL __saveds ASM FXPlug_rendergfx_rp(register __d1 struct RastPort *rp, register __d2 struct Screen *scr, register __d3 struct fsbitmap *fsb, register __d4 long xpos, register __d5 long ypos, register __d6 struct PluginInit *pli)
  452. {
  453.         initfunc(pli);
  454.  
  455.         /* Demo-"Driver" drawing black boxes instead of graphical content */
  456.         /* Active PLG_DISPLAYDRIVER in the flags to test this one .. */
  457.  
  458.         SetAPen(rp,1);
  459.         RectFill(rp, xpos, ypos, xpos+fsb->width, ypos+fsb->height);
  460.  
  461.         return(TRUE);
  462. }
  463.  
  464. BOOL __saveds ASM FXPlug_PluginSetRegister (register __d1 struct PluginReg *pr)
  465. {
  466.         return(FALSE);
  467. }
  468.  
  469.